/**
 * 十六进制字符串转字节数组
 * 每2个字符串转换
 * 100102030405060708 转为 [16, 1, 2, 3, 4, 5, 6, 7, 8]
 * @param {String} str 符合16进制字符串
 */
function Str2Bytes(str) {
    var pos = 0;
    var len = str.length;
    if (len % 2 != 0) {
        return null;
    }
    len /= 2;
    var hexA = new Array();
    for (var i = 0; i < len; i++) {
        var s = str.substr(pos, 2);
        var v = parseInt(s, 16);
        hexA.push(v);
        pos += 2;
    }
    return hexA;
}

// console.log(Str2Bytes("EB902255FFBD0000333926003840602E63").reduce((sum, item) => sum + item, 0));

function toUint8Array(inputArray) {
    const result = [];

    inputArray.forEach((num) => {
        if (num <= 0xFF) {
            result.push(num);
        } else {
            const bytes = [];
            while (num > 0) {
                bytes.unshift(num & 0xFF)
                num >>= 8;
            }
            result.push(...bytes);
        }
    });

    return new Uint8Array(result);
}

function toHexString(uint8Array) {
    return Array.from(uint8Array)
        .map(byte => byte.toString(16).padStart(2, '0'))
        .join(' ');
}

// EB902255FFBD0000333926003840602E63
const input = [0xeb90, 0x22, 0x55, 0xff, 0xbd, "0x0000", "0x00", 0x33, 0x3926, 0x00384060, 0x2E63];
const uint8Array = toUint8Array(input)
console.log(uint8Array)
const checksum = uint8Array.slice(5, 260).reduce((a, b) => a + b, 0) & 0xff;
console.log(checksum.toString(16));


console.log(toHexString(uint8Array)); // 输出 Uint8Array(6) [ 235, 144, 0, 34, 0, 68 ]
